package admin.forms;

import helpers.StringHelper;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import admin.models.ApplicationModel;

import entities.Application;


public class ApplicationForm {
	public static TreeMap checkValid(HttpServletRequest request, Application app) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String id = (String) request.getParameter("id");
		String title = (String) request.getParameter("title");
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		
		if (app.getId().isEmpty()) {
			if (id == null || id.isEmpty()) {
				check.put("id", "Please enter id.");
				app.setId("");
			} else if (appModel.checkExists(id)) {
				check.put("id", "Id application has been exists.");
				app.setId(id);
			} else {
				app.setId(id);
			}
		}
		
		if (title == null || title.isEmpty()) {
			check.put("title", "Title is not null.");
			app.setTitle("");
		} else {
			app.setTitle(title);
		}
		
		form.put("checkValid", check);
		form.put("app", app);
		
		return form;
	}
}
